﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Clock
{
    class Program
    {
        public delegate void ClockHanlder(object sender, ClockEventArgs args);
        public class ClockEventArgs
        {
            public int Hour { get; set; }
            public int Minute { get; set; }
            public int Second { get; set; }
        }

        public class Clock
        {
            public event ClockHanlder Tick;
            public event ClockHanlder Alarm;
            private int hour, minute, second, aHour, aMinure;
            public Clock(int hour, int minute)  //构造函数设置时间
            {
                if (hour < 60 && minute < 60)
                {
                    this.hour = hour;
                    this.minute = minute;
                    this.second = 0;
                }
                else
                    this.hour = this.minute = this.second = 0;
            }
            public void SetClock(int hour, int minute)  //设置闹钟
            {
                aHour = hour;
                aMinure = minute;
            }
            public void Run()
            {
                while (true)
                {
                    this.second++;
                    if (this.second >= 60)
                    {
                        this.second = 0;
                        this.minute++;
                    }
                    if (this.minute >= 60)
                    {
                        this.minute = 0;
                        this.hour++;
                    }
                    if (this.hour >= 24)
                        this.hour = 0;
                    ClockEventArgs arg = new ClockEventArgs() { Hour = hour, Minute = minute, Second = second };
                    if (this.hour == this.aHour && this.minute == this.aMinure)
                        Alarm(this, arg);
                    else
                        Tick(this, arg);
                    Thread.Sleep(1000);
                }
            }
        }

        public class MyClock
        {
            public Clock myClock;
            public MyClock(int hour, int minute)
            {
                myClock = new Clock(hour, minute);
                myClock.Tick += new ClockHanlder(Btn_Tick);
                myClock.Alarm += Btn_Alarm;
            }
            void Btn_Tick(object sender, ClockEventArgs args)
            {
                Console.WriteLine($"{args.Hour}:{args.Minute}:{args.Second},Tick");
            }
            void Btn_Alarm(object sender, ClockEventArgs args)
            {
                Console.WriteLine($"{args.Hour}:{args.Minute}:{args.Second},Alarm");
            }
        }
        static void Main(string[] args)
        {
            MyClock myClock = new MyClock(6, 30);   //设置时间6：30
            myClock.myClock.SetClock(6, 31);     //设置闹钟；
            myClock.myClock.Run();
        }
    }
}
